/**
 * todo 上传xml文件到服务器 方便调试
 * @param title 
 * @param name 
 */

import { httpPost } from "./httpPost";
import { useDeviceUuid } from "./useDeviceUuid";
import { useRoot } from "./useRoot";

export function uploadXml(title: string, name: string) {
    const root = useRoot()
    if(!root) return;
    const xml = astToString(root)
    return httpPost('addPage', { title, name, content: xml, deviceUuid: useDeviceUuid(), packageName: root.packageName(), activityName: currentActivity() })
}

export function uploadJson(title: string, name: string) {
    const root = useRoot()
    if(!root) return;
    const xml = JSON.stringify(astToJson(root), null, 2)
    return httpPost('addPage', { title, name, content: xml, deviceUuid: useDeviceUuid(), packageName: root.packageName(), activityName: currentActivity() })
}

export function astToJson(root: AutoJs.UiObject) {
    if (!root.visibleToUser) return;
    const className = root.className()
    const id = root.id()
    const text = root.text()
    const clickable = root.clickable()
    const selected = root.selected()
    const childCount = root.childCount()
    const desc = root.desc()
    const editable = root.editable
    const visibleToUser = root.visibleToUser()
    const scrollable = root.scrollable()
    let children: any[] = []
    for (let i = 0; i < childCount; i++) {
        const child = root.child(i)
        if (child) {
            const res = astToJson(child)
            if (res) {
                children.push(res)
            }
            child.recycle()
        }
    }
    return {
        className,
        id,
        text,
        selected,
        clickable,
        childCount,
        desc,
        editable,
        visibleToUser,
        scrollable,
        children
    };
}

export function astToString(root: AutoJs.UiObject) {
    if (!root.visibleToUser()) return ``
    const className = root.className()
    const id = root.id()
    const text = root.text()
    const clickable = root.clickable()
    const selected = root.selected()
    const childCount = root.childCount()
    const desc = root.desc()
    const editable = root.editable
    const visibleToUser = root.visibleToUser()
    let children = ``
    for (let i = 0; i < childCount; i++) {
        const child = root.child(i)
        if (child) {
            children += astToString(child)
            child.recycle()
        }
    }
    let idStr = id ? " id='" + id + "'" : ""
    let textStr = text ? " text='" + text + "'" : ""
    let clickableStr = clickable ? " clickable='" + clickable + "'" : ""
    let selectedStr = selected ? " selected='" + selected + "'" : ""
    let descStr = desc ? ` desc=${desc}` : ``
    let editableStr = editable ? ` editable=${editable}` : ``
    let visibleToUserStr = visibleToUser ? ` visibleToUser=${visibleToUser}` : ``

    const html = `<${className}${idStr}${textStr}${clickableStr}${selectedStr}${descStr}${editableStr}${visibleToUserStr}>
        ${children}
    </${className}>`
    return html;
}
